
using System;
using UnityEngine;
using UnityEngine.UIElements;

public class PatrolState : IState
{
    private FSM manager;
    private Parameter parameter;
    private int currentIndex = 0;

    //在构造函数中获取到状态机的引用和参数
    public PatrolState(FSM manager)
    {
        this.manager = manager;
        parameter = manager.parameter;
    }

    public void OnEnter()
    {
        manager.animator.Play("Walk");
    }

    public void OnUpdate()
    {
        manager.Flip(parameter.patrolPoints[currentIndex]);
        manager.transform.position = Vector2.MoveTowards(manager.transform.position, 
                parameter.patrolPoints[currentIndex].position,parameter.moveSpeed*Time.deltaTime);
        
        if (parameter.isHit)
        {
            manager.TransitionState(StateType.Hit);
        }
        
        if (Vector2.Distance(manager.transform.position, parameter.patrolPoints[currentIndex].position)<0.1f)
        {
            manager.TransitionState(StateType.Idle);
        }

        

        if (null !=parameter.playerTrans && parameter.playerTrans.position.x>parameter.chasePoints[1].position.x
                                         &&parameter.playerTrans.position.x<parameter.chasePoints[0].position.x)
        {
            manager.TransitionState(StateType.React);
        }
    }

    public void OnExit()
    {
        currentIndex++;
        if (currentIndex>=parameter.patrolPoints.Length)
        {
            currentIndex = 0;
        }
        
    }
}
